package com.applabs.dnd.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckedTextView;

public class Rule extends Activity implements OnClickListener{

	int type;
	CheckedTextView chkNoOne;
	CheckedTextView chkKnown;
	CheckedTextView chkGroups;
	CheckedTextView chkFavourites;
	CheckedTextView chkWhiteList;
	CheckedTextView chkSameAsCall;
	//for DND behaviour , shud be radio button
	CheckedTextView chkSilent;
	CheckedTextView chkReject;
	CheckedTextView chkRejectWithMsg;
	
	Button btnOK;
	ArrayList<String> checked = new ArrayList<String>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		final Intent intent = getIntent();
		type = intent.getIntExtra("Type", -1);
		
		if ( type == 1 )
		{
			setContentView(com.applabs.dnd.R.layout.call_rule);
		}
		else if ( type == 2 )
		{
			setContentView(com.applabs.dnd.R.layout.msg_rule);
		}
		else
			setContentView(com.applabs.dnd.R.layout.dnd_behaviour);
		
		if ( type == 1 || type == 2 )
		{
			//get Views
			chkNoOne = (CheckedTextView)findViewById(com.applabs.dnd.R.id.NoOne);
			chkKnown = (CheckedTextView)findViewById(com.applabs.dnd.R.id.Known);
			chkGroups = (CheckedTextView)findViewById(com.applabs.dnd.R.id.Groups);
			chkFavourites = (CheckedTextView)findViewById(com.applabs.dnd.R.id.Favourites);
			chkWhiteList = (CheckedTextView)findViewById(com.applabs.dnd.R.id.WhiteList);
			
			if ( type == 2 )
				chkSameAsCall = (CheckedTextView)findViewById(com.applabs.dnd.R.id.SameAsCall);
			
			// set Listeners
			chkNoOne.setOnClickListener(this);
			chkKnown.setOnClickListener(this);
			chkGroups.setOnClickListener(this);
			chkFavourites.setOnClickListener(this);
			chkWhiteList.setOnClickListener(this);
			if(type == 2)
				chkSameAsCall.setOnClickListener(this);
		}
		else
		{
			//for DND behaviour
			
			// get views
			chkSilent = (CheckedTextView)findViewById(com.applabs.dnd.R.id.Silent);
			chkReject = (CheckedTextView)findViewById(com.applabs.dnd.R.id.Reject);
			chkRejectWithMsg = (CheckedTextView)findViewById(com.applabs.dnd.R.id.RejectWithMsg);
			
			// set Listeners
			chkSilent.setOnClickListener(this);
			chkReject.setOnClickListener(this);
			chkRejectWithMsg.setOnClickListener(this);


		}
		
		// For OK button
		findViewById(com.applabs.dnd.R.id.ok).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//String selected = checked.toString();
				intent.putStringArrayListExtra("selected", checked);
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (type != 3)
		{
			findViewById(v.getId());
			if(((CheckedTextView)v).isChecked())
			{
				//chkNoOne.setChecked(false);
				((CheckedTextView)v).setChecked(false);
				//chkNoOne.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
				checked.remove(((CheckedTextView)v).getText());
				((CheckedTextView)v).setBackgroundColor(Color.WHITE);
				

			}else
			{
				((CheckedTextView)v).setChecked(true);
				//chkNoOne.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
				checked.add((String) ((CheckedTextView)v).getText());
				((CheckedTextView)v).setBackgroundColor(Color.GRAY);

			}
		}
		else
		{
			findViewById(v.getId());
			if(((CheckedTextView)v).isChecked())
			{
				//chkNoOne.setChecked(false);
				((CheckedTextView)v).setChecked(false);
				//chkNoOne.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
				checked.remove(((CheckedTextView)v).getText());
				((CheckedTextView)v).setBackgroundColor(Color.WHITE);
				

			}else
			{
				((CheckedTextView)v).setChecked(true);
				//chkNoOne.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
				checked.add((String) ((CheckedTextView)v).getText());
				((CheckedTextView)v).setBackgroundColor(Color.GRAY);

			}
		}
		
	}
	

}
